import { get } from 'utils/request.js';
import { config } from 'config.js';

let configs = config();
// 获取应用实例
const app = getApp(); //  eslint-disable-line no-undef
var mta = require('../../utils/mta_analysis.js');

Page({
  data: {
    scrollHeight: 0,
    navigateHeight: 0,
    map:{
      zoom: 14,
      markers: [],
      polyline: [],
      steps: []
    },
    name: '',
    distance: '0米',
    distances: '0',
    duration: '0分钟',
    currentLatitude: null,
    currentLongitude: null,
    steps: [],
    onLine: true,
    noAuth: false,
    id: null,
    name: '',
    iconPathMap: {
      'green':  require('../../images/com/green.png'),
      'greenSelected':  require('../../images/com/green-selected.png'),

      'yellow':  require('../../images/com/yellow.png'),
      'yellowSelected':  require('../../images/com/yellow-selected.png'),

      'red':  require('../../images/com/red.png'),
      'redSelected':  require('../../images/com/red-selected.png')
    },
    toLatitude: null,
    toLongitude: null,
    iconType: null
  },
  regionchange(e) {
      console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },
  onLoad(options) {
    // 调用应用实例的方法获取全局数
    var latitude =  options.latitude;             //         厕所位置；
    var longitude =  options.longitude;
   
    var toLatitude = options.currentLatitude;     //              当前位置；
    var toLongitude = options.currentLongitude;   

    var name = options.name;
    var iconType = options.icontype;


console.log(name);
    var res = wx.getSystemInfoSync();
    var navigateHeight = res.screenHeight - 352;
    var scrollHeight = navigateHeight - 79;


    this.setData({
        scrollHeight: scrollHeight,
        navigateHeight: navigateHeight,
        currentLatitude: latitude,
        currentLongitude: longitude,
        toLatitude: toLatitude,
        toLongitude: toLongitude,
        name: name,
        iconType: iconType
    });  

    //统计
    mta.Page.init();

  },
  onShow(e){
    var that = this;
    that.loadToilets();

  },
  onReady(){
    this.mapCtx = wx.createMapContext('Map');
  },
  loadToilets(){
    var that = this;
    console.log('....................................');
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        console.log(latitude)
        console.log(longitude)
        // that.data.currentLatitude =  latitude;
        // that.data.currentLongitude = longitude;
        that.getData({from_lat: latitude, from_lng: longitude, to_lat: that.data.toLatitude, to_lng: that.data.toLongitude}, that);
      },
      fail: function(res){

        //关闭loading
        wx.hideLoading();
        //没有权限
        that.setData({
          noAuth: true
        });
      }
    })

  },
  getData(obj, context){
    wx.showLoading({
      title: '加载中'
    });
    get({'path': configs.navi}, obj).then(function(res){
        wx.hideLoading();
        var data = res.data.data;
        var points = [];
        data.routes.polyline.map(function(el, index){
          var point = {
            latitude: el.lat,
            longitude: el.lng
          }
          points.push(point)
        })

        var polyline = [{
          color:"#FFD937",
          width: 6,
          points: points
        }]
   

        var icon_type = context.data.iconType || 1;
        console.log(icon_type)

        var tempIconPath;
        if(icon_type == 1){
          tempIconPath = context.data.iconPathMap.greenSelected;
        }else if(icon_type == 2){
          tempIconPath = context.data.iconPathMap.yellowSelected;
        }else if(icon_type == 3){
          tempIconPath = context.data.iconPathMap.redSelected;
        }

        var markers = [{
          latitude: data.routes.to.lat,
          longitude: data.routes.to.lng,
          width: 48,
          height: 58,
          iconPath: tempIconPath
        }];

        var steps = data.routes.steps;

        context.setData({
          'map.polyline': polyline,
          'map.markers': markers,
          'map.steps': steps,
          'map.zoom': data.routes.zoom,
          distance: data.routes.distance,
          distances: data.distances,
          name: context.data.name,
          duration: data.routes.duration,
          currentLatitude: context.data.currentLatitude,
          currentLongitude:  context.data.currentLongitude
        })
        
        context.mapCtx.moveToLocation();
        // // //关闭loading
        // wx.hideLoading();
        
    }, function(res){
      //关闭loading
      wx.hideLoading();
      //有可能是参数有问题或者是网络
      context.setData({
        onLine: false,
        noAuth: false
      });
    })
  },
  onRefreshTap(e){
    var that = this;
    that.setData({
      onLine: true
    });
    that.loadToilets();
  },
  goPersonalCenter(e){
    wx.navigateTo({
      url: '/pages/complaint-list/complaint-list'
    })
  },
  setGps(e){
    var that = this;
    that.mapCtx = wx.createMapContext('Map');
    that.mapCtx.moveToLocation();
  },
  openLocationTap(e){
    var latitude = parseFloat(e.currentTarget.dataset.latitude);
    var longitude = parseFloat(e.currentTarget.dataset.longitude);
    var name = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    wx.showLoading({
      title: '加载中'
    });
    wx.openLocation({
     latitude: latitude,
     longitude: longitude,
     name: name,
     address: address,
     scale: 18,
     success: function(){
       wx.hideLoading();
     }
    })
  }
});
